package org.dgm.nw.core.viewControl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 对actionMapping.xml 进行解析
 * ActionMappingParse 
 *  
 *
 * Copyright asiainf-linkage Corporation 2013
 */
public class ActionMappingParse {

	static Logger log = Logger.getLogger(ActionMappingParse.class);

	private static ActionMappingParse actionMappingParse;

	Document document;

	Element rootElm;

	private ActionMappingParse(String FILE_NAME) {
		try {
			SAXReader reader = new SAXReader();
			// URL urlFile =
			// Thread.currentThread().getContextClassLoader().getResource("");
			// //到达class
			document = reader.read(Thread.currentThread()
					.getContextClassLoader().getResourceAsStream(FILE_NAME));
			rootElm = document.getRootElement();
			log.info("根结点名称=" + rootElm.getName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static ActionMappingParse getInstance() {
		if (actionMappingParse == null) {
			actionMappingParse = new ActionMappingParse("actionMapping.xml");
		}
		return actionMappingParse;
	}

	public Document getDocument() {
		return document;
	}

	public Element getRootElm() {
		return rootElm;
	}
	
	
	/**
	 * 生成 子元素 的map,以指定的column的值为key,element为value
	 * @param fatherEle
	 * @param column
	 * @return 
	 * Map<String,Element>
	 */
	public static Map<String, Element> generateChildrenElementMapByColumn(Element fatherEle,String column){
		List elementList = fatherEle.elements();
		log.info(elementList);
		
		Map<String, Element> elementMap = new HashMap<String, Element>();
		for (int i = 0; i < elementList.size(); i++) {
			Element tmpElement = (Element) elementList.get(i);
			String targetColumn = tmpElement.attributeValue(column);
			elementMap.put(targetColumn, tmpElement);
		}
		return elementMap;
	}

	
}
